﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using System.Drawing;
using System.IO;
using DataLayer.Model;

namespace GameUpWS.DTOs
{
    public class ServiceProfile : Profile
    {
        static public byte[] imageToByteArray(string path, string imageUrl)
        {
            if(string.IsNullOrWhiteSpace(imageUrl))
                return new byte[0];
            Image imageIn = Image.FromFile("C:/inetpub/wwwroot/GameUpWS/images/"+ path + imageUrl);
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }

        protected override void Configure()
        {
            Mapper.CreateMap<Player, PlayerDTO>()
                .ForMember(t => t.Name, f => f.MapFrom(r => r.Name))
                .ForMember(t => t.Surname, f => f.MapFrom(r => r.Surname))
                .ForMember(t => t.IsFemale, f => f.MapFrom(r => r.IsFemale))
                .ForMember(t => t.Thumbnail, f => f.MapFrom(r => imageToByteArray("thumbnails/", r.ThumbnailUrl)))
                .ReverseMap();

            Mapper.CreateMap<Administrator, AdministratorDTO>()
                .ForMember(t => t.Name, f => f.MapFrom(r => r.Name))
                .ForMember(t => t.Surname, f => f.MapFrom(r => r.Surname))
                .ForMember(t => t.IsFemale, f => f.MapFrom(r => r.IsFemale))
                .ReverseMap();

            Mapper.CreateMap<Professional, ProfessionalDTO>()
                .ForMember(t => t.Name, f => f.MapFrom(r => r.Name))
                .ForMember(t => t.Surname, f => f.MapFrom(r => r.Surname))
                .ForMember(t => t.IsFemale, f => f.MapFrom(r => r.IsFemale))
                .ReverseMap();

            Mapper.CreateMap<Message, PrivateMessageDTO>()
                .ForMember(t => t.Sender, f => f.MapFrom(r => r.IdReceiver.Name + " " + r.IdReceiver.Surname))
                .ReverseMap();

            Mapper.CreateMap<IndoorExercisePersonal, IndoorExerciseDTO>()
                .ForMember(t => t.ExerciseType, f => f.MapFrom(r => r.IndoorExerciseType.Name))
                .ForMember(t => t.Parameters, f => f.MapFrom(r => r.Parameters.Split(',')))
                .ReverseMap();

            Mapper.CreateMap<OutdoorExercise, OutdoorExerciseDTO>()
                .ForMember(t => t.StepsGoal, f => f.MapFrom(r => r.Steps))
                .ForMember(t => t.FloorsGoal, f => f.MapFrom(r => r.Floors))
                .ForMember(t => t.DistanceGoal, f => f.MapFrom(r => r.Distance))
                .ReverseMap();

            Mapper.CreateMap<FitbitData, OutdoorRAWDataDTO>()
                .ForMember(t => t.DateTime, f => f.MapFrom(r => r.Date))
                .ReverseMap();

            Mapper.CreateMap<PlayerGroup, GroupDTO>()
                .ForMember(t => t.Members, f => f.MapFrom(r => r.Players))
                .ReverseMap();

            Mapper.CreateMap<OutdoorExerciseSocial, OutdoorExerciseSocialDTO>()
                .ForMember(t => t.Type, f => f.MapFrom(r => r.Type))
                .ForMember(t => t.StepsGoal, f => f.MapFrom(r => r.OutdoorExercise.Steps))
                .ForMember(t => t.FloorsGoal, f => f.MapFrom(r => r.OutdoorExercise.Floors))
                .ForMember(t => t.DistanceGoal, f => f.MapFrom(r => r.OutdoorExercise.Distance))
                .ReverseMap();

            Mapper.CreateMap<OutdoorExerciseSocial_CheerMessage, CheerMessageDTO>()
                .ForMember(t => t.IdSender, f => f.MapFrom(r => r.Player.Id))
                .ForMember(t => t.SenderName, f => f.MapFrom(r => r.Player.Name + " " + r.Player.Surname))
                .ReverseMap();

            Mapper.CreateMap<CheerMessage, CheerMessageContentDTO>()
                .ReverseMap();

            Mapper.CreateMap<CheerMessagesGroup, CheerMessageGroupDTO>()
                .ForMember(t => t.Messages, f => f.MapFrom(r => Mapper.Map<IEnumerable<CheerMessage>, List<CheerMessageContentDTO>>(r.CheerMessages)))
                .ReverseMap();

            base.Configure();
        }
    }
}